#include <stdio.h>
#include <stdlib.h>
#include "search.h"

// Function to print an array
void printArray(int data[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", data[i]);
    }
    printf("\n");
}

// Function to test sequential search
void testSequentialSearch() {
    int data[] = {10, 20, 30, 40, 50};
    int size = sizeof(data) / sizeof(data[0]);
    int key = 30;
    int result = sequential_search(data, size, key);
    if (result != -1) {
        printf("Sequential search: Found %d at index %d\n", key, result);
    }
    key = 60;
    result = sequential_search(data, size, key);
    if (result == -1) {
        printf("Sequential search: %d not found\n", key);
    }
}

// Function to test binary search
void testBinarySearch() {
    int data[] = {10, 20, 30, 40, 50};
    int size = sizeof(data) / sizeof(data[0]);
    int key = 30;
    int result = binary_search(data, size, key);
    if (result != -1) {
        printf("Binary search: Found %d at index %d\n", key, result);
    }
    key = 60;
    result = binary_search(data, size, key);
    if (result == -1) {
        printf("Binary search: %d not found\n", key);
    }
}

// Function to test binary search tree creation and insertion
void testBSTInsertion() {
    int data[] = {50, 30, 20, 40, 70, 60, 80};
    int size = sizeof(data) / sizeof(data[0]);
    bs_tree bst = bst_create(data, size);
    printf("BST created with elements: ");
    // In a real - world scenario, you may want to implement an in - order traversal to print the BST elements.
    // For simplicity, we just indicate it's created here.
    printf("(created)\n");
    bst_free(bst);
}

// Function to test binary search tree search
void testBSTSearch() {
    int data[] = {50, 30, 20, 40, 70, 60, 80};
    int size = sizeof(data) / sizeof(data[0]);
    bs_tree bst = bst_create(data, size);
    int key = 40;
    bst_node *result = bst_search(bst, key);
    if (result != NULL) {
        printf("BST search: Found %d in the tree\n", key);
    }
    key = 90;
    result = bst_search(bst, key);
    if (result == NULL) {
        printf("BST search: %d not found in the tree\n", key);
    }
    bst_free(bst);
}

// Function to test binary search tree deletion
void testBSTDeletion() {
    int data[] = {50, 30, 20, 40, 70, 60, 80};
    int size = sizeof(data) / sizeof(data[0]);
    bs_tree bst = bst_create(data, size);
    int key = 40;
    int result = bst_delete_node(&bst, key);
    if (result == 0) {
        printf("BST deletion: Deleted %d from the tree\n", key);
    }
    key = 90;
    result = bst_delete_node(&bst, key);
    if (result == -1) {
        printf("BST deletion: %d not found in the tree for deletion\n", key);
    }
    bst_free(bst);
}

int main() {
    // Test sequential search
    testSequentialSearch();

    // Test binary search
    testBinarySearch();

    // Test binary search tree insertion
    testBSTInsertion();

    // Test binary search tree search
    testBSTSearch();

    // Test binary search tree deletion
    testBSTDeletion();

    return 0;
}